package com.example.fw;

public class ContactHelper extends HelperBase{

	public ContactHelper(ApplicationManager applicationManager) {
		super(applicationManager);
	}

	public void createContact(Contact contact) {
		initContactCreation();
		fillContactForm(contact);
		confirmContactCreation();
	}


	private void initContactCreation() {
		manager.getAutoItHelper()
		  .winWaitAndActivate("AddressBook Portable", "", 15000)
		  .click("Add").winWaitAndActivate("Add Contact", "", 5000);
	}

	private void fillContactForm(Contact contact) {
		manager.getAutoItHelper()
		  .send("TDBEdit12", contact.firstname)
		  .send("TDBEdit11", contact.lastname);
	}
	
	private void confirmContactCreation() {
		manager.getAutoItHelper()
		  .click("Save")
		  .winWaitAndActivate("AddressBook Portable", "", 5000);
	}

	public Contact getFirstContact() {
		selectAndEdit();
		Contact contact = saveContact();
		cancelEdit();
		return contact;
	}
	
	private void selectAndEdit() {
		manager.getAutoItHelper()
		  .winWaitAndActivate("AddressBook Portable", "", 15000)
		  .click("TListView1")
		  .send("{DOWN}{SPACE}")
		  .click("Edit")
		  .winWaitAndActivate("Update Contact", "", 5000);
	}

	private Contact saveContact() {
		Contact contact = new Contact()
		  .setFirstName(manager.getAutoItHelper().getText("TDBEdit12"))
		  .setLastName(manager.getAutoItHelper().getText("TDBEdit11"));
		return contact;
	}
	
	private void cancelEdit() {
		manager.getAutoItHelper()
		  .click("Cancel")
		  .winWaitAndActivate("AddressBook Portable", "", 12000);
	}

	public void removeContact() {
		manager.getAutoItHelper()
		  .winWaitAndActivate("AddressBook Portable", "", 15000)
		  .click("TListView1")
		  .send("{DOWN}{SPACE}")
		  .click("Delete")
		  .winWaitAndActivate("Confirm", "", 20000)
		  .send("{ENTER}")
		  .winWaitAndActivate("AddressBook Portable", "", 15000);
	}


}
